<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @Copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-2-25下午3:53:13
 *
 */
 
 
 class Post_model extends CI_Model{
 	
 	public function __construct(){
 		parent::__construct();
 		$this->load->database();
 	}
 	
 	/**
 	 * 插入一条记录
 	 * @access public 
 	 * @param array $field 字段值数组
 	 * @return int|bool
 	 */
 	public function insert($field, $table){
 		$this->db->insert($table, $field);
 		if($this->db->affected_rows() > 0){
 			return $this->db->insert_id();
 		}else{
 			return false;
 		}
 	}

 	/**
 	 * 更新一条记录
 	 * @param array $field 字段值数组
 	 * @param string $table 表名
 	 * @param array $condition 条件数组 "="
 	 * @return bool
 	 */
 	public function update($field, $table, $condition = ''){
 		if(!empty($condition)){
 			while(list($k, $v) = each($condition)){
 				$this->db->where($k, $v);
 			}
 		}
 		$this->db->update($table, $field);
 		if($this->db->affected_rows() > 0){
 			return true;
 		}else{
 			return false;
 		}
 		
 	}
 	
 	/**
 	 * 更新taxonomy表中的count字段
 	 * @access public
 	 * @param int $tid description
 	 * @param int $var description
 	 * @return void description
 	 */
 	 public function update_count($tid,$var){
 	 	$this->db->select('count');
 	 	$this->db->from('taxonomy');
 	 	$this->db->where('tid', $tid);
 	 	$query = $this->db->get();
 	 	$row = $query->row_array();
 	 	
 	 	$this->db->where('tid', $tid);
 	 	$this->db->update('taxonomy', array('count'=>$row['count']+$var));
 	 }
 	/**
 	 * 删除一条记录
 	 * @param string $table 表名
 	 * @param array $condition 条件 "="
 	 * @return bool
 	 */
 	public function delete($table, $condition){
 		if(!empty($condition)){
 			while(list($k, $v) = each($condition)){
 				$this->db->where($k, $v);
 			}
 		}
 		$this->db->delete($table);
 		if($this->db->affected_rows() > 0){
 			return true;
 		}else{
 			return false;
 		} 		
 	}
 	/**
 	 * 获取多条记录
 	 * @param array $field
 	 * @param string $table
 	 * @param array $conditions description
 	 * @return array|bool
 	 */
 	public function get_all($field, $table, $conditions = ''){
 		$this->db->select($field);
 		$this->db->from($table);
 		if(!empty($conditions)){
 			foreach($conditions as $k=>$v){
 				$this->db->where($k, $v);
 			}
 		}
 		$this->db->order_by('addTime', 'DESC');
 		$query = $this->db->get();
 		if($query->num_rows > 0){
 			return $query->result_array();
 		}else{
 			return false;
 		}
 	}
	
 	/**
 	 * 获取一条记录
 	 * @param array $field
 	 * @param string $table
 	 * @param array $condition
 	 * @return array|bool
 	 */
 	public function get_one($bpid){
 		$this->db->select('*');
 		$this->db->from('blog_post');
 		$this->db->where('bpid', $bpid);
 		$query = $this->db->get();
 		return $query->row_array();
 	}
	 /**
	  * 获取所有分类的树(二级)，分类ID，分类名
	  * @access public
	  * @param string|array $args 查询的字段
	  * @return array $tree 分类树
	 */
	 public function get_category_tree($args){
	 	//id slug parent
	   $tree = array();
		$this->db->select($args);
		$this->db->from('taxonomy');
		$this->db->where('module', 'blog');
		$this->db->where('type', 'category');
		$query = $this->db->get();
		$rows = $query->result_array();
		foreach($rows as $row){
			if($row['parent'] == 0){
				$tree[$row['tid']]['parent_category'] =  $row;
			}else{
				$tree[$row['parent']]['sub_category'][$row['tid']] = $row; 
			}
		}
		return $tree;
	 } 
	 
	 /**
	  * 获取标签的ID
	  * @param string $tag 标签名
	  * @return int 标签的ID
	  */
	 public function get_tag_id($tag){
	 	$this->db->select('tid');
	 	$this->db->from('taxonomy');
	 	$this->db->where('name', $tag);
	 	$query = $this->db->get();
	 	if($query->num_rows > 0){
	 		$row = $query->row_array();
	 		return $row['tid'];
	 	}else{
	 		return 0;
	 	}
	 }
	 
	 
	 public function get_category($bpid){
	 	$sql = 'SELECT b.tid, b.name ';
	 	$sql .='FROM '.$this->db->dbprefix('tax_map').' a ,'.$this->db->dbprefix('taxonomy').' b ';
	 	$sql .= 'WHERE a.tid = b.tid AND b.type = \'category\' AND b.module=\'blog\' AND a.pid='.$bpid;
	 	$query = $this->db->query($sql);
	 	//一维数组
	 	return $query->row_array();
	 }
	 
	 public function get_tags($bpid){
	 	$sql = 'SELECT b.name ';
	 	$sql .='FROM '.$this->db->dbprefix('tax_map').' a ,'.$this->db->dbprefix('taxonomy').' b ';
	 	$sql .= 'WHERE a.tid = b.tid AND b.type = \'tag\' AND b.module=\'blog\' AND a.pid='.$bpid;
	 	$query = $this->db->query($sql);
	 	//一维 二维数组
	 	return $query->result_array();
	 }
 }
 
/* End of file post_model.php  */
/* Loaction : ./apps/admin/models/blog/post_model.php */
